import os
# 安装 
#pip install snownlp -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 
# 导入 
from snownlp import SnowNLP

import Pinyin2Hanzi 
from xpinyin import Pinyin

def alter(file,old_str,new_str):
    """
    将替换的字符串写到一个新的文件中，然后将原文件删除，新文件改为原来文件的名字
    :param file: 文件路径
    :param old_str: 需要替换的字符串
    :param new_str: 替换的字符串
    :return: None
    """
    with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
        for line in f1:
            if old_str in line:
                line = line.replace(old_str, new_str)
            f2.write(line)
    os.remove(file)
    os.rename("%s.bak" % file, file)

def alter_and_append(source_file,destination_file, old_str,new_str):
    """
    将替换的字符串写到一个新的文件中，然后将原文件删除，新文件改为原来文件的名字
    :param file: 文件路径
    :param old_str: 需要替换的字符串
    :param new_str: 替换的字符串
    :return: None
    """
    with open(source_file, "r", encoding="utf-8") as f1,open(destination_file, "a", encoding="utf-8") as f2:
        sn = 0
        '''
        old_str : list
        old_str[0]: 
        '''
        for line in f1:
            if old_str in line:
                line = line.replace(old_str, new_str)
            f2.write(line)
        
    
        f1.close()
        f2.close()


    # 打开文件并将内容追加到末尾
    #file = open("example.txt", "a")
    #file.write("追加的内容")
    #file.close()

 
#alter("file1", "python", "测试")


#先导入

#简单使用一下
p = Pinyin() 
result = p.get_pinyin('小琳爱分享')  #此处结果：xiao-lin-ai-fen-xiang
result = p.get_pinyin('小琳爱分享','') #此处结果：xiaolinaifenxiang
result = p.get_pinyin('小琳爱分享',' ') #此处结果：xiao lin ai fen xiang
print(result) #结果：xiao-lin-ai-fen-xiang

def file_to_pinyin(hanzi_file,pinyin_file):
    with open(hanzi_file, "r", encoding="utf-8") as f1,open(pinyin_file, "w", encoding="utf-8") as f2:
        for line in f1:
            #s = SnowNLP('小王爱分享')
            # line 是persons.txt里面的人名
            s = SnowNLP(line)
            #print(s.pinyin) #结果：['xiao', 'wang', 'ai', 'fen', 'xiang']
            #将list转为字符串
            #result = s.pinyin[0].capitalize() + ' ' + ''.join(s.pinyin[1:]).capitalize() 
            #result = s.pinyin[0].capitalize()  + ''.join(s.pinyin[1:]).capitalize() +'\n'
            #print(result) #结果：Xiao Wangaifenxiang
            '''
            #取首字母
            from snownlp import SnowNLP
            s = SnowNLP('小王爱分享')
            result = ''.join([i[0].upper() for i in s.pinyin])
            print(result) #结果：XWAFX
            '''
            p=Pinyin()
            result=p.get_pinyin(line,"")
            print(result)
            f2.write(result)
# 获取当前目录
directory = os.getcwd()
print(directory)

# 读 persons.txt ，转化为 拼音
kanji_file = "C:\\joget_artifat\\persons.txt"
pinyin_file = "C:\\joget_artifat\\persons_pinyin.txt"
file_to_pinyin( kanji_file,pinyin_file)
persons_pinyin_list  =[]

#
template_file   = "template.txt"
output_file     = "output.txt"
#
#for item in persons_pinyin_list:
#    alter( )

